define(function(require, exports, module) {
    "use strict";

    if (typeof jQuery == 'undefined') {
        return;
    }
    var _artdlg_plus = require('artdialog_plus') || function() {};
    var __tempRules = {
        'alf_username': /^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){4,19}$/,
        'alf_userpwd': /^[\w~!@#$%^&*()_+{}:<>?\-=[\];\.\/]{6,16}$/
    };

    //[Method] 初始化表单
    var __initializeForm = function() {
        var _formObj = $("#adminlogin_form"),
            _btnObj = $("#btnSubmit");

        if (_formObj.length == 0 || _btnObj.length == 0) {
            return;
        }

        $.Tipmsg.r = null;
        var __showMsg = function(msg, o) {
            var tempFunc = function() {};
            if (o.type == 3) {
                if (o.obj.attr("type") == "text" || o.obj.attr("type") == "password") {
                    tempFunc = function() {
                        o.obj.trigger("focus");
                    }
                }
                $.zex.utils.artAlert(_artdlg_plus, '提示', msg, 1, '', tempFunc);
            }
        }

        _formObj.Validform({
            btnSubmit: '#btnSubmit',
            tiptype: function(msg, o, cssctl) {
                if (!o.obj.is("form")) {
                    __showMsg(msg, o);
                }
            },
            datatype: $.isPlainObject(__tempRules) ? $.extend({}, __tempRules, $.zex.regular) : $.zex.regular,
            callback: function(form) {
                $(form).ajaxSubmit({
                    beforeSubmit: __aformAlways,
                    success: __aformDone,
                    error: __aformFail,
                    url: 'admin_ajax.ashx?action=check_login',
                    type: "POST",
                    dataType: "json",
                    cache: false,
                    timeout: 60000
                });

                function __aformAlways(formData, jqForm, options) {
                    _btnObj.prop("disabled", true).removeClass('disabled').addClass('disabled').val('正在提交...');
                }

                function __aformDone(data, textStatus) {
                    data = eval(data)
                    if (parseInt(data.status) == 1) {
                        _btnObj.val("操作成功");
                        var __s_dlg = _artdlg_plus({
                            content: '登录成功！正在转向，请稍等...',
                            backdropBackground: '#267cb7',
                            backdropOpacity: 0.5
                        }).showModal();
                        setTimeout(function() {
                            $("#btnReset").click();
                            __s_dlg.close().remove();
                            window.location.replace('index.aspx');
                        }, 2000);
                    } else {
                        var _ctl_name, _ctl_flag;
                        _ctl_name = String(data.eletag).indexOf('|') > -1 ? $.zex.utils.CLeft(data.eletag, '|') : '';
                        _ctl_flag = String(data.eletag).indexOf('|') > -1 ? $.zex.utils.CRight(data.eletag, '|') : data.eletag;

                        $.zex.utils.artAlert(_artdlg_plus, '提示', unescape(data.msg), 1, '', function() {
                            switch (_ctl_flag) {
                                case "captchas":
                                    $("#txtCaptchas").focus();
                                    $.zex.utils.reloadVCode('img_verifyCode', $("#img_verifyCode").attr('src'));
                                    break;
                                default:
                                    window.location.reload();
                                    break;
                            }
                        });
                        _btnObj.prop("disabled", false).removeClass('disabled').val("登录");
                    }
                }

                function __aformFail(XHR, textStatus, errorThrown) {
                    $.zex.utils.artAlert(_artdlg_plus, '提示', '网络超时，请稍后再试！', 1, '', null);
                    _btnObj.prop("disabled", false).removeClass('disabled').val("登录");
                }
                return false;
            },
            tipSweep: true,
            showAllError: false
        });
    };

    $(document).ready(function() {
        __initializeForm();
        $("#img_verifyCode").bind('click', function(e) {
            e.stopPropagation();
            $.zex.utils.reloadVCode('img_verifyCode', $(this).attr('src'));
        });
        /*
        $(document).keyup(function(event) {
            if (event.keyCode === 13) {
                $("#btnSubmit").trigger("click");
            }
        });
        */
    });

});